package ericXie.com.component 
{
	import ericXie.com.events.PopUpWindowEvent;
	import ericXie.com.model.MainModel;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.display.SimpleButton
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	/**
	 * ...弹出窗口
	 * @author EricXie
	 */
	public class PopUpWindowMovieClip extends MovieClip 
	{
		//弹出窗口
		private var _popUpWindow:MovieClip;
		private var _model:MainModel = MainModel.getInstance();
		//标题文本
		private var _titleText:String;
		//信息文本
		private var _contentText:String;
		//超链接文本
		private var _linkText:String;
		//标题文本框
		private var titleTextField:TextField = new TextField();
		//信息文本框
		private var contentTextField:TextField = new TextField();
		//超链接文本框
		private var linkTextField:TextField = new TextField();
		//标题文本格式
		private var titleTextFormat:TextFormat;
		//信息文本格式
		private var contentTextFormat:TextFormat;
		//超链接文本格式
		private var linkTextFormat:TextFormat;
		//文本默认宽度
		private var textWidth:Number = 270;
		//行间距
		private var spacing:Number = 5;
		
		public function PopUpWindowMovieClip() 
		{
			this.addEventListener(Event.ADDED_TO_STAGE, addStageHandler);
			initTextFormat();
		}
		
		private function addStageHandler(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, addStageHandler);
			_popUpWindow = _model.extractionElements("PopUpWindow") as MovieClip;
			this.addChild(_popUpWindow);
			addTextField();
			SimpleButton(_popUpWindow.CloseBn).addEventListener(MouseEvent.CLICK, closeWindowHandler);
			SimpleButton(_popUpWindow.OkButton).addEventListener(MouseEvent.CLICK, closeWindowHandler);
		}
		//初始化设置各文本格式
		private function initTextFormat():void 
		{
			titleTextFormat = new TextFormat("宋体", 18, 0xfe0000, true);
			titleTextFormat.align = TextFormatAlign.CENTER;
			contentTextFormat = new TextFormat("宋体", 12, 0x010101);
			linkTextFormat = new TextFormat("宋体", 12, 0x3299f9, false, false, true);
		}
		//添加各个文本框，并且设置格式，设置鼠标为不可操作。多行，及宽度
		private function addTextField():void 
		{
			this.addChild(titleTextField)
			this.addChild(contentTextField);
			this.addChild(linkTextField);
			titleTextField.defaultTextFormat=titleTextFormat;
			contentTextField.defaultTextFormat=contentTextFormat;
			linkTextField.defaultTextFormat = linkTextFormat;
			titleTextField.mouseEnabled = contentTextField.mouseEnabled  = false;
			titleTextField.wordWrap = true;
			titleTextField.width = contentTextField.width = linkTextField.width = textWidth;
		}
		//更新文本框相对位置及大小。始终保持居中位置。当某文本框数据缺失时，设置该文本框高度为0；
		private function updataSize():void
		{
			//titleTextField.x = getCenterX(titleTextField);
			titleTextField.x =(_popUpWindow.width - textWidth) / 2;
			contentTextField.x = getCenterX(contentTextField);
			linkTextField.x = getCenterX(linkTextField);
			var sumHeight:Number = getTextHeight(titleTextField) + getTextHeight(contentTextField) + getTextHeight(linkTextField);
			var _top:Number=(_popUpWindow.height - sumHeight) / 2
			titleTextField.y = _top;
			contentTextField.y = _top + getTextHeight(titleTextField);
			linkTextField.y = _top + getTextHeight(titleTextField) + getTextHeight(contentTextField);
			linkTextField.height = getTextHeight(linkTextField);
		}
		//返回文本框的横向居中坐标
		private function getCenterX(_textField:TextField):Number 
		{
			var centerX:Number = (_popUpWindow.width - _textField.textWidth) / 2;
			return centerX;
		}
		//返回文本框高度
		private function getTextHeight(_textField:TextField):Number 
		{
			if (_textField.text == "" || _textField.text == null) {
				return 0;
			}else {
				return _textField.textHeight+spacing;
			}
		}
		//关闭窗口
		private function closeWindowHandler(e:MouseEvent):void 
		{
			new PopUpWindowEvent(PopUpWindowEvent.POPDOWN_TYPE).dispatch();
		}
		
		public function get titleText():String 
		{
			return _titleText;
		}
		
		public function set titleText(value:String):void 
		{
			_titleText = value;
			titleTextField.text = _titleText;
			updataSize();
		}
		
		public function get contentText():String 
		{
			return _contentText;
		}
		
		public function set contentText(value:String):void 
		{
			_contentText = value;
			contentTextField.text = _contentText;
			updataSize();
		}
		
		public function get linkText():String 
		{
			return _linkText;
		}
		
		public function set linkText(value:String):void 
		{
			_linkText = value;
			linkTextField.htmlText = linkText;
			updataSize();
		}
		
	}

}